﻿using System;
using System.Text;
using System.IO;
using System.Net;

namespace DataAccess
{
    public class Public
    {
        static public void WriteLog(string msg)
        {
            string logFolder = AppDomain.CurrentDomain.BaseDirectory + "Log";
            if (!Directory.Exists(logFolder))
            {
                Directory.CreateDirectory(logFolder);
            }
            string logFile = logFolder + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            try
            {
                using (StreamWriter sw = File.AppendText(logFile))
                {
                    string fullMsg = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + " " + msg;
                    sw.WriteLine(fullMsg);
                    Console.WriteLine(fullMsg);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception e)
            {
                using (StreamWriter sw = File.AppendText(logFile))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + " WriteLog Exception: " + e.Message);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
                throw;
            }
        }

        static public void WriteAllBytes(string folder, string name, byte[] bytes)
        {
            try
            {
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                    WriteLog(folder + " doesn't exist! try to CreateDirectory");
                }
                if (!File.Exists(folder + name))
                {
                    File.WriteAllBytes(folder + name, bytes);
                }
                else
                {
                    WriteLog(folder + name + " doe exist!");
                }
            }
            catch (Exception e)
            {
                WriteLog("WriteAllBytes Exception: " + e.Message);
                throw;
            }
        }

        /// <summary>
        /// 提交文章链接到百度
        /// </summary>
        /// <param Name="Url">文章的链接</param>
        /// <param Name="IsOriginal">是否为原创</param>
        public static string PostBaidu(string url, string strStie, string strToken)
        {
            string urlPage = "http://data.zz.baidu.com/urls?site=" + strStie + "&token=" + strToken + "";
            WriteLog("PostBaidu urlPage: " + urlPage);

            Encoding encoding = Encoding.GetEncoding("utf-8");
            byte[] data = encoding.GetBytes(url);               //这里采用utf-8编码

            //设置提交的参数         
            HttpWebRequest request = WebRequest.Create(urlPage) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "text/plain";
            request.ContentLength = data.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(data, 0, data.Length);
                requestStream.Flush();
                requestStream.Close();
            }

            //测试返回结果 
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = (HttpWebResponse)ex.Response;
            }
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            WriteLog("PostBaidu retString: " + retString);
            return retString;
        }
    }
}
